

let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let logger = require('morgan');
//let FileUtil = require('./utils/file')
import FileUtil from "./utils/file"

let indexRouter = require('./routes/index');
let usersRouter = require('./routes/users');
//c++测试
//let addon = require("./db/addon")
// //console.log(addon.hello())

let BeanFactory = require("./package/BeanFactory");
Reflect.set(global, 'bean', BeanFactory);
//配置注入表
BeanFactory.setInjectionRelation(['KvClient']);



let app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


//中间件
//参数
app.use(require('./middleware/ParamAndRes'))

app.use('/api',require('./routes/users'))
app.use('/api', indexRouter);
//代理
app.use('/api', require('./routes/proxy'));
//身份验证
app.use(require('./middleware/AuthorToken'))
//权限访问
app.use(require('./middleware/PermissionFilter'))
app.use('/api', usersRouter);
//web端api
app.use('/api', require('./routes/web'));
//手机端api
app.use('/api', require('./routes/mobile'));
//错误处理
app.use(require('./middleware/Error'));


//获取access_token
// setTimeout(async () => {
//    const token = FileUtil.readFile('./token.txt');
//    if (!token) {
//       const dingToken = await global['bean']['DingApi'].getAccess_Token();
//       global['ding_token'] = dingToken.data.access_token;
//       FileUtil.writeFile('.', 'token.txt', dingToken.data.access_token);
//    } else {
//       const stat = FileUtil.getFileState('./token.txt');
//
//       if (new Date().getTime() - stat.mtime.getTime() > 1000 * 60 * 60 * 1.5) {
//          const dingToken = await global['bean']['DingApi'].getAccess_Token();
//          global['ding_token'] = dingToken.data.access_token;
//          FileUtil.writeFile('.', 'token.txt', dingToken.data.access_token);
//       } else {
//          global['ding_token'] = token;
//       }
//
//    }
//
// }, 1.5 * 3600)

module.exports = app;
